package ciyuan.Player.Skill.Passive;

import ciyuan.Player.Skill.MySkill;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class AddStar extends MySkill {

    private static final String SUBSTRING = ",starCount";
    private static final String LastTime = ",starTime";

    @Override
    public void keyDown(PlayerEntity playerEntity) { }

    @Override
    public String getSkillCastName() {
        return "addStar";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.9");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.9");
    }

    public static void addCount(PlayerEntity playerEntity) {
        if (SkillUtils.getSaveData(playerEntity,SUBSTRING)>=15) {
            return;
        }
        SkillUtils.setSaveData(playerEntity,SUBSTRING,SkillUtils.getSaveData(playerEntity,SUBSTRING) + 1);
    }

    @SubscribeEvent
    public static void onDamage(LivingDamageEvent event) {
        if (event.getEntityLiving() instanceof ServerPlayerEntity) {
            PlayerEntity playerEntity = (PlayerEntity) event.getEntityLiving();
            if (playerEntity.getHealth()-event.getAmount()<=0) {
                if (SkillUtils.getSaveData(playerEntity,SUBSTRING)>=5 && System.currentTimeMillis()-SkillUtils.getSaveData(playerEntity,LastTime)>180000) {
                    SkillUtils.setSaveData(playerEntity,SUBSTRING,SkillUtils.getSaveData(playerEntity,SUBSTRING)-5);
                    SkillUtils.setSaveData(playerEntity,LastTime,System.currentTimeMillis());
                    event.setAmount(0);
                    SkillUtils.setSkillColdInterval(playerEntity,"addStar",180*1000);
                    SkillUtils.saveSkillCastTime(playerEntity,"addStar");
                }
            }
        }
    }
}
